(function ($, doc) {
    var userinfo = app.getState();
    var uid = userinfo.user_id;
    $.init({
        swipeBack:true,
        gestureConfig: {
            tap: true, //默认为true
            doubletap: true, //默认为false
            longtap: true, //默认为false
            swipe: true, //默认为true
            drag: true, //默认为true
            hold: true, //默认为false，不监听
            release: true //默认为false，不监听
        }
    });
    template.config('escape', false);

    var msgTap = function (e) {
        var el  = this;
        var val = el.getAttribute('favid');
        plus.nativeUI.actionSheet({
            buttons: [ {
                title: "删除"
            }]
        }, function(e) {
            console.log("User pressed: " + e.index);
            if(e.index == 1) {
                var toUrl = apiurl + 'Friends/delFav';
                mui.post(toUrl, {
                    id : val
                }, function(res){
                    if(res.status == 1) {
                        document.querySelector('#fav').removeChild(el);
                    }
                })
            }
            return true;
        })
    }

    $.plusReady(function () {
        var user = app.getState();
        var url = apiurl + 'favorite/index?uid=' + user.user_id;
        $.get(url, function (data) {
            renderlist(data);
        })

        function renderlist(data){
            for(var i in data.favorites){
                if(data.favorites[i].header == '' || data.favorites[i].header == null){
                    data.favorites[i].header = '../../static/images/1.png';
                }else{
                    data.favorites[i].header = serverurl + data.favorites[i].header;
                }
            }
            document.querySelector("#fav").innerHTML = template('list-template', {
                "data": data.favorites
            });
        }
        mui(".mui-bar-nav").on("tap", "#search", function() {
            mui.openWindow({
                url: "search.html",
                id: "search.html"
            })
        });

        mui(".mui-bar-nav").on("tap", "#notes", function() {
            mui.openWindow({
                url: "notes.html",
                id: "notes.html"
            })
        });

        mui("#fav").on("tap", ".fav-sub", function() {
            mui.openWindow({
                url: "note_detail.html",
                id: "note_detail.html",
                extras: {
                    uid: this.getAttribute('uid'),
                    cid: this.getAttribute('cid'),
                    cname: this.getAttribute('cname'),
                    header: this.getAttribute('header'),
                    ctype: this.getAttribute('ctype'),
                    c_msg: this.getAttribute('c_msg'),
                    c_img: this.getAttribute('c_img'),
                    ctime: this.getAttribute('ctime')
                }
            })
        });

        $('#fav').on('longtap', '.fav-sub', msgTap);

    })
}(mui, document));